package com.yuntsg.nnsfcp.service.front.ut;

import cn.hutool.core.date.DateTime;
import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * @author ：ljx
 * @date ：Created in 2019/11/9
 */
public class DateUtil {

    /**
     * 获取当前年 字符串类型
     *
     * @return
     */
    public static String getCurrentYearStr() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR) + "";
    }

    /**
     * 返回当月第一天
     *
     * @return
     */
    public static String longToFirstDay() {
        String str = "";
        Calendar cale = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        str = format.format(cale.getTime());
        return str;
    }

    /**
     * 返回今天的的yyyy-MM-dd
     *
     * @return
     */
    public static String todayToyyyyMMdd() {
        String str = "";
        Calendar cale = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale = Calendar.getInstance();
        str = format.format(cale.getTime());
        return str;
    }

    /**
     * 返回上个月的今天
     *
     * @return
     */
    public static String currentDayBackMonth() {
        DateTime dateTime = cn.hutool.core.date.DateUtil.lastMonth();
        String s = dateTime.toString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return s;
    }

    /**
     * 输入long 返回yyyy-MM-dd的日期
     *
     * @param i
     * @return
     */
    public static String longToDate_ymd(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }

    /**
     * 输入long 返回yyyy的日期
     *
     * @param i
     * @return
     */
    public static String longToDate_y(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }

    /**
     * 输入long 返回yyyy年MM月的日期
     *
     * @param i
     * @return
     */
    public static String longToDate_ym(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }


    /**
     * 输入long 返回年月日 时分秒
     *
     * @param i
     * @return
     */
    public static String longToDate1(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 返回今天的下一天
     *
     * @return
     */
    public static String nextDay_ymd() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);
        return sf.format(c.getTime());
    }


    /**
     * 返回今天的上一天
     *
     * @return
     */
    public static String backOneDay_ymd() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        return sf.format(c.getTime());
    }

    /**
     * 输入字符时间 返回long
     *
     * @param str
     * @return
     */
    public static long StringToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    public static LocalDateTime StringToDateLocalDateTime(String str) {
        DateTime parse = cn.hutool.core.date.DateUtil.parse(str);
        long time = parse.getTime();
        LocalDateTime localDateTime = parseLongtoLocalDateTime(time);
        System.out.println("转化后时间+" + localDateTime);
        return localDateTime;
    }

    public static void main(String[] args) {
        String datestr = "2024年05月01日";
        String datestr2 = "2024年05月";
        String datestr3 = "2024";

        String s1 = todayToyyyyMMdd();
        System.out.println(s1);
        LocalDateTime localDateTime = StringToDateLocalDateTime(datestr);
        System.out.println(localDateTime);
        Long l = yyyy年MM月ToLong(datestr2);
        Long l3 = yyyyToLong(datestr3);

        String s = longToDate_ymd(System.currentTimeMillis());
        String s2 = longToDate_ymd(l);
        String s3 = longToDate_ymd(l3);
        System.out.println(s);
        System.out.println(s2);
        System.out.println(s3);

        LocalDateTime dateTime = parseLongtoLocalDateTime(todayStartLong());
        LocalDateTime dateTime2 = parseLongtoLocalDateTime(todayEndLong());
        System.out.println(dateTime);
        System.out.println(dateTime2);


    }

    //根据当前时间获取当天的时间开始的时间
    public static long todayStartLong() {
        String s = longToDate_ymd(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return ts;
    }

    //根据当前的时间获取当天的结束时间
    public static long todayEndLong() {
        DateTime dateTime = cn.hutool.core.date.DateUtil.offsetDay(new Date(), 1);
        String s2 = dateTime.toString();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(s2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parse.getTime();
        return time;

    }


    public static LocalDateTime parseLongtoLocalDateTime(Long l) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault());
        return localDateTime;
    }


    /**
     * 输入字符时间 返回long
     *
     * @param str
     * @return
     */
    public static long StringToDateH(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 输入localdate 返回yyyy年MM月dd日
     *
     * @param localDate
     * @return
     */
    public static String LocalDateToString(LocalDate localDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String res = localDate.format(formatter);
        return res;
    }

    /**
     * 输入localdate 返回yyyy年MM月
     *
     * @param localDate
     * @return
     */
    public static String LocalDateToStringyyyymm(LocalDateTime localDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月");
        String res = localDate.format(formatter);
        return res;
    }

    /**
     * 输入 yyyy年MM月 返回时间戳
     *
     * @param localDate
     * @return
     */
    public static Long yyyy年MM月ToLong(String localDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        Date date = null;
        try {
            date = (Date) sdf.parse(localDate);
            return date.getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    /**
     * 输入 yyyy年 返回时间戳
     *
     * @param localDate
     * @return
     */
    public static Long yyyyToLong(String localDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = null;
        try {
            date = (Date) sdf.parse(localDate);
            return date.getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    public static String LocalDateToStringyyyymmDD(LocalDateTime localDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String res = localDate.format(formatter);
        return res;
    }


    public static LocalDateTime parseLong(Long l) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault());
        return localDateTime;
    }

    /**
     * 输当前日期往后推半年的日期
     *
     * @return
     */
    public static String nowToHalfYear() {
        String res = "";
        Date now = new Date();
        DateTime dateTime = cn.hutool.core.date.DateUtil.offsetDay(now, 182);
        res = dateTime.toString("yyyy年MM月dd日");
        return res;
    }

    /**
     * 格式化localDateTime 中文的年月日
     *
     * @param localDate
     * @return
     */
    public static String LocalDateTimeToString(LocalDateTime localDate) {
        String res = "";
        if (localDate == null) {
            return res;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        res = localDate.format(formatter);
        return res;
    }


    /**
     * 格式化localDateTime 中文的年月日
     *
     * @param localDate
     * @return
     */
    public static String LocalDateTimeToStringDefault(LocalDateTime localDate) {
        String res = "";
        if (localDate == null) {
            localDate = LocalDateTime.now();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        res = localDate.format(formatter);
        return res;
    }


    /**
     * 获取当前年  int类型
     *
     * @return
     */
    public static Integer getCurrentYearInt() {
        Calendar calendar = Calendar.getInstance();
        Integer intYear = Integer.valueOf(calendar.get(Calendar.YEAR) + "");
        return intYear;
    }

    public static String longToDate_ymdhms() {
        long i = System.currentTimeMillis();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    public static String longToDate_yyyymmdd() {
        long i = System.currentTimeMillis();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 获取年月日 字符串类型
     *
     * @return
     */
    public static String getCurrentYmdStr() {
        //获取年月日,作为"文件夹"的名字
        Calendar calendar = Calendar.getInstance();
        return "" + calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DATE);
    }

    /**
     * 生成当前时间格式的字符串,作为文件名
     * "yyyy-MM-dd-HH-mm-ss"
     *
     * @param format "yyyy-MM-dd-HH-mm-ss"
     * @return
     */
    public static String getFormatYearStr(String format) {
        if (StringUtils.isEmpty(format)) {
            format = "yyyy-MM-dd-HH-mm-ss";
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 将英文月份简写, 转换为数字月份
     *
     * @param month 英文月
     * @return
     */
    public static String getMonthInt(String month) {
        if (!StringUtils.isEmpty(month)) {
            HashMap<String, String> monthEnMap = new HashMap<>(16);
            monthEnMap.put("jan", "1");
            monthEnMap.put("feb", "2");
            monthEnMap.put("mar", "3");
            monthEnMap.put("apr", "4");
            monthEnMap.put("may", "5");
            monthEnMap.put("jun", "6");
            monthEnMap.put("jul", "7");
            monthEnMap.put("aug", "8");
            monthEnMap.put("sept", "9");
            monthEnMap.put("oct", "10");
            monthEnMap.put("nov", "11");
            monthEnMap.put("dec", "12");
            String monthNum = monthEnMap.get(month.toLowerCase());
            if (!StringUtils.isEmpty(monthNum)) {
                return monthNum;
            }
        }
        return "";
    }
}
